﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace MyCommerce.Business.Stores
{
    public class ConveyorManager
    {
        private MyCommerceEntities vContext;

        public ConveyorManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public ConveyorManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<Conveyor> GetConveyors()
        {
            List<Conveyor> colConveyors = new List<Conveyor>();

            var result = from st in vContext.Conveyor
                         select st;
            if (result != null)
                colConveyors = new List<Conveyor>(result.AsEnumerable<Conveyor>());

            return colConveyors;
        }
        
        public Conveyor GetConveyorByID(long IdConveyor)
        {
            Conveyor objConveyor = new Conveyor();
            var result = (from st in vContext.Conveyor
                         where st.IdConveyor == IdConveyor
                         select st).First();
            objConveyor = (Conveyor)result;
            return objConveyor;
        }

        public void SaveConveyor(Conveyor objConveyor)
        {
            var validateResult = (from cl in vContext.Conveyor
                                  where (cl.FullName.CompareTo(objConveyor.FullName) == 0)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un pilotero con este nombre.");
            }

            vContext.AddToConveyor(objConveyor);
            vContext.SaveChanges();
        }

        public void UpdateConveyor(Conveyor objConveyor)
        {
            var validateResult = (from cl in vContext.Conveyor
                                  where (cl.FullName.CompareTo(objConveyor.FullName) == 0 && cl.IdConveyor != objConveyor.IdConveyor)
                                  select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un pilotero con este nombre.");
            }

            var vResult = (from st in vContext.Conveyor
                           where st.IdConveyor == objConveyor.IdConveyor
                           select st).First();

            vResult.Code = objConveyor.Code;
            vResult.FullName = objConveyor.FullName;
            vResult.Address = objConveyor.Address;
            vResult.Phone = objConveyor.Phone;
            vResult.Observation = objConveyor.Observation;

            vContext.SaveChanges();
        }

        public void DeleteConveyor(long IdConveyor)
        {
            var vResult = (from st in vContext.Conveyor
                           where st.IdConveyor == IdConveyor
                           select st).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }

    }
}
